extends Control

@onready var health_label = $TopPanel/PlayerInfo/HealthLabel
@onready var energy_label = $TopPanel/PlayerInfo/EnergyLabel
@onready var gold_label = $TopPanel/PlayerInfo/GoldLabel
@onready var floor_label = $TopPanel/PlayerInfo/FloorLabel

@onready var combat_button = $MapContainer/EventButtons/CombatButton
@onready var elite_button = $MapContainer/EventButtons/EliteButton
@onready var rest_button = $MapContainer/EventButtons/RestButton
@onready var shop_button = $MapContainer/EventButtons/ShopButton

@onready var deck_button = $BottomPanel/ActionButtons/DeckButton
@onready var relics_button = $BottomPanel/ActionButtons/RelicsButton
@onready var potions_button = $BottomPanel/ActionButtons/PotionsButton

var game_data: GameData

func _ready():
	game_data = GameData.get_instance()
	
	# 连接按钮信号
	combat_button.pressed.connect(_on_combat_pressed)
	elite_button.pressed.connect(_on_elite_pressed)
	rest_button.pressed.connect(_on_rest_pressed)
	shop_button.pressed.connect(_on_shop_pressed)
	
	deck_button.pressed.connect(_on_deck_pressed)
	relics_button.pressed.connect(_on_relics_pressed)
	potions_button.pressed.connect(_on_potions_pressed)
	
	_update_ui()

func _update_ui():
	# 更新玩家信息显示
	health_label.text = "生命值: " + str(game_data.player_health) + "/" + str(game_data.player_max_health)
	energy_label.text = "能量: " + str(game_data.player_energy) + "/" + str(game_data.player_max_energy)
	gold_label.text = "金币: " + str(game_data.player_gold)
	floor_label.text = "第 " + str(game_data.current_floor) + " 层"
	
	# 根据楼层显示可用事件
	_update_event_availability()

func _update_event_availability():
	# 重置所有按钮
	combat_button.visible = true
	elite_button.visible = false
	rest_button.visible = true
	shop_button.visible = true
	
	# 精英怪楼层 (4, 8, 12)
	if game_data.current_floor in [4, 8, 12]:
		elite_button.visible = true
	
	# Boss楼层 (15)
	if game_data.current_floor == 15:
		combat_button.visible = false
		elite_button.visible = false
		rest_button.visible = false
		shop_button.visible = false

func _on_combat_pressed():
	# 进入普通战斗
	_start_combat(false)

func _on_elite_pressed():
	# 进入精英战斗
	_start_combat(true)

func _start_combat(is_elite: bool):
	game_data.in_battle = true
	game_data.current_enemies.clear()
	
	if is_elite:
		# 创建精英怪
		_create_elite_enemies()
	else:
		# 创建普通怪物
		_create_normal_enemies()
	
	# 切换到战斗场景
	get_tree().change_scene_to_file("res://scenes/BattleScene.tscn")

func _create_normal_enemies():
	# 根据楼层创建普通怪物
	var enemy_names = ["数学题", "语文题", "英语题", "物理题", "化学题"]
	var exam_types = ["数学考试", "语文考试", "英语考试", "物理考试", "化学考试"]
	
	var enemy_count = 1
	if game_data.current_floor > 5:
		enemy_count = 2
	if game_data.current_floor > 10:
		enemy_count = 3
	
	for i in enemy_count:
		var name = enemy_names[randi() % enemy_names.size()]
		var exam = exam_types[randi() % exam_types.size()]
		var health = 20 + game_data.current_floor * 3
		var damage = 5 + game_data.current_floor
		
		var enemy = Enemy.new(name, health, damage, exam, game_data.current_floor)
		game_data.current_enemies.append(enemy)

func _create_elite_enemies():
	# 创建精英怪
	var elite_names = ["期末考试", "模拟考试", "升学考试", "资格证考试"]
	var elite_exams = ["期末考试", "模拟考试", "升学考试", "资格证考试"]
	
	var name = elite_names[randi() % elite_names.size()]
	var exam = elite_exams[randi() % elite_exams.size()]
	var health = 60 + game_data.current_floor * 8
	var damage = 12 + game_data.current_floor * 2
	
	var enemy = Enemy.new(name, health, damage, exam, game_data.current_floor)
	enemy.is_elite = true
	game_data.current_enemies.append(enemy)

func _on_rest_pressed():
	# 进入汉府休息
	_show_rest_event()

func _show_rest_event():
	# 恢复生命值和能量
	game_data.player_health = game_data.player_max_health
	game_data.player_energy = game_data.player_max_energy
	
	# 显示休息界面
	var dialog = AcceptDialog.new()
	dialog.title = "汉府"
	dialog.dialog_text = "你在汉府好好休息了一番，恢复了所有生命值和能量。"
	dialog.add_button("继续", true)
	add_child(dialog)
	dialog.popup_centered()
	
	dialog.confirmed.connect(func(): dialog.queue_free())

func _on_shop_pressed():
	# 进入吾悦广场商店
	_show_shop_event()

func _show_shop_event():
	# 显示商店界面
	var dialog = AcceptDialog.new()
	dialog.title = "吾悦广场"
	dialog.dialog_text = "欢迎来到吾悦广场！这里可以购买卡牌、遗物和药水。\n(商店功能待实现)"
	dialog.add_button("离开", true)
	add_child(dialog)
	dialog.popup_centered()
	
	dialog.confirmed.connect(func(): dialog.queue_free())

func _on_deck_pressed():
	# 显示卡组信息
	var dialog = AcceptDialog.new()
	dialog.title = "我的卡组"
	var deck_info = "卡组数量: " + str(game_data.deck.size()) + "\n"
	deck_info += "手牌数量: " + str(game_data.hand.size()) + "\n"
	deck_info += "弃牌堆: " + str(game_data.discard_pile.size())
	dialog.dialog_text = deck_info
	dialog.add_button("关闭", true)
	add_child(dialog)
	dialog.popup_centered()
	
	dialog.confirmed.connect(func(): dialog.queue_free())

func _on_relics_pressed():
	# 显示遗物信息
	var dialog = AcceptDialog.new()
	dialog.title = "我的遗物"
	var relics_info = "遗物数量: " + str(game_data.relics.size()) + "\n"
	if game_data.relics.size() == 0:
		relics_info += "暂无遗物"
	else:
		for relic in game_data.relics:
			relics_info += "• " + relic.name + ": " + relic.description + "\n"
	dialog.dialog_text = relics_info
	dialog.add_button("关闭", true)
	add_child(dialog)
	dialog.popup_centered()
	
	dialog.confirmed.connect(func(): dialog.queue_free())

func _on_potions_pressed():
	# 显示药水信息
	var dialog = AcceptDialog.new()
	dialog.title = "我的药水"
	var potions_info = "药水数量: " + str(game_data.potions.size()) + "\n"
	if game_data.potions.size() == 0:
		potions_info += "暂无药水"
	else:
		for potion in game_data.potions:
			potions_info += "• " + potion.name + ": " + potion.description + "\n"
	dialog.dialog_text = potions_info
	dialog.add_button("关闭", true)
	add_child(dialog)
	dialog.popup_centered()
	
	dialog.confirmed.connect(func(): dialog.queue_free())
